<?php

/*
 * Copyright (C) 2010 Emanuele Colonnelli <emanuele.colonnelli@phpstruct.org>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
namespace org\phpstruct\util;

/**
 * Description of StdLib
 *
 * @author Emanuele Colonnelli <emanuele.colonnelli@phpstruct.org>
 */
class StdLib {

    public static function normalizePath($path){
        $ds = \DIRECTORY_SEPARATOR;
        if($ds == '\\'){
            //WIN
            return \str_replace('/', \DIRECTORY_SEPARATOR, $path);
        } else {
            return \str_replace('\\', \DIRECTORY_SEPARATOR, $path);
        }
    }

    public static function toDefaultCase($str) {
        if (defined('APP_CASE')) {
            switch (APP_CASE) {
                case \PDO::CASE_UPPER:
                    return \strtoupper($str);
                    break;
                case \PDO::CASE_LOWER:
                    return \strtolower($str);
                    break;
            }
        }
    }

    public static function redirect($url) {
        die(\header("Location:{$url}"));
    }

//:-

    public static function truncateByWords($string, $max_words) {
        $phrase_array = \explode(' ', $string);
        if (\count($phrase_array) > $max_words && $max_words > 0)
            $string = \implode(' ', \array_slice($phrase_array, 0, $max_words)) . '...';
        return $string;
    }

//:-

    /**
     * Check if there are empty post variables recursively and return an array without empty values
     *
     * @param array $data
     * @return array
     */
    public static function cleanupEmptyFormData($data) {
        $ret = array();
        \array_walk_recursive($data, array('Rebel_Stdlib', 'checkIfIsEmpty'), $ret);
        return $ret;
    }

//:-

    private static function checkIfIsEmpty($v, $k, &$ret) {
        return \trim($v) || ('0' == $v) ? $ret[$k] = $v : null;
    }

    public static function getUniqId() {

        return \md5(\uniqid(\rand(), true));
    }

//:-

    public static function checkAndGet($variable, $nullIfUnset=true) {
        return isset($variable) ? $variable : null;
    }

    public static function getUploadMaxFilesize() {
        $sSize = \ini_get('upload_max_filesize');
        $sUnit = \substr($sSize, -1);
        $iSize = (int) \substr($sSize, 0, -1);
        switch (\strtoupper($sUnit)) {
            case 'Y' : $iSize *= 1024; // Yotta
            case 'Z' : $iSize *= 1024; // Zetta
            case 'E' : $iSize *= 1024; // Exa
            case 'P' : $iSize *= 1024; // Peta
            case 'T' : $iSize *= 1024; // Tera
            case 'G' : $iSize *= 1024; // Giga
            case 'M' : $iSize *= 1024; // Mega
            case 'K' : $iSize *= 1024; // kilo
        };
        return $iSize;
    }

    function sec2hms($sec, $padHours = false) {
        $hms = "";
        // there are 3600 seconds in an hour, so if we
        // divide total seconds by 3600 and throw away
        // the remainder, we've got the number of hours
        $hours = \intval(\intval($sec) / 3600);
        // add to $hms, with a leading 0 if asked for
        $hms .= ( $padHours) ? \str_pad($hours, 2, "0", \STR_PAD_LEFT) . ':' : $hours . ':';
        // dividing the total seconds by 60 will give us
        // the number of minutes, but we're interested in
        // minutes past the hour: to get that, we need to
        // divide by 60 again and keep the remainder
        $minutes = \intval(($sec / 60) % 60);
        // then add to $hms (with a leading 0 if needed)
        $hms .= \str_pad($minutes, 2, "0", \STR_PAD_LEFT) . ':';
        // seconds are simple - just divide the total
        // seconds by 60 and keep the remainder
        $seconds = \intval($sec % 60);
        // add to $hms, again with a leading 0 if needed
        $hms .= \str_pad($seconds, 2, "0", \STR_PAD_LEFT);
        return $hms;
    }

//

    public static function toUppercaseArray($data, $uppercase_keys=true, $uppercase_values=true) {
        $result = Array();

        foreach ($data as $k => $v) {
            if ($uppercase_keys)
                $k = \strtoupper($k);
            if ($uppercase_values)
                $v = \strtoupper($v);
            $result[$k] = $v;
        }

        return $result;
    }

}

